package cn.itcast.hotel.web;

import cn.itcast.hotel.mqconstants.MqHotelConstants;
import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.PageResult;
import cn.itcast.hotel.pojo.UserBean;
import cn.itcast.hotel.service.IHotelService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import javax.servlet.http.HttpSession;
import java.security.InvalidParameterException;

@CrossOrigin
@RestController
@RequestMapping("hotel")
public class HotelController {

    @Autowired
    private IHotelService hotelService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/{id}")
    public Hotel queryById(@PathVariable("id") Long id){
        return hotelService.getById(id);
    }

    @GetMapping("/list")
    public PageResult hotelList(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "1") Integer size)
    {
        Page<Hotel> result = hotelService.page(new Page<>(page, size));

        return new PageResult(result.getTotal(), result.getRecords());
    }

    @PostMapping
    public void saveHotel(@RequestBody Hotel hotel){
        hotelService.save(hotel);

        rabbitTemplate.convertAndSend(
                MqHotelConstants.HOTEL_EXCHANGE_TOPIC,
                MqHotelConstants.HOTEL_INSERT,
                hotel.getId()
                );
    }

    @PutMapping()
    public void updateById(@RequestBody Hotel hotel){
        if (hotel.getId() == null) {
            throw new InvalidParameterException("id不能为空");
        }
        hotelService.updateById(hotel);

        rabbitTemplate.convertAndSend(
                MqHotelConstants.HOTEL_EXCHANGE_TOPIC,
                MqHotelConstants.HOTEL_INSERT,
                hotel.getId()
        );
    }

    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable("id") Long id) {
        hotelService.removeById(id);

        rabbitTemplate.convertAndSend(
                MqHotelConstants.HOTEL_EXCHANGE_TOPIC,
                MqHotelConstants.HOTEL_DELETE,
                id
        );
    }

    /**
     * 登录/短信验证登录
     * @param user
     * @param userPhone
     * @return
     */
    @RequestMapping("/login")
    public String login(UserBean user,HttpSession session,String userPhone){

        return hotelService.login(user,session,userPhone);
    }

    /**
     * 注册
     * @param user
     * @return
     */
    @RequestMapping("/register")
    public String register(UserBean user){

        return hotelService.register(user);
    }

    /**
     * 修改密码
     * @param userId
     * @return
     */
    @RequestMapping("changePassword")
    public UserBean changePassword(Integer userId){

        return hotelService.changePassword(userId);
    }

   /* *//**
     * 短信验证登录
     * @param userPhone
     * @return
     *//*
    @RequestMapping("sendCode")
    public String sendCode(String userPhone){

        return hotelService.sendCode(userPhone);
    }*/



}
